﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace UtfprLibrary.Windows.Forms {
    [ToolboxItem(false)]
    public class UtfprButtonWithMouseOver : UtfprButtonBase {
        #region Base

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            if (IsMouseOver) {
                var drawRectangle = new Rectangle(
                    e.ClipRectangle.X,
                    e.ClipRectangle.Y,
                    e.ClipRectangle.Width - 1,
                    e.ClipRectangle.Height - 1);

                var fillRectangle = new Rectangle(
                    e.ClipRectangle.X + 1,
                    e.ClipRectangle.Y + 1,
                    e.ClipRectangle.Width - 2,
                    e.ClipRectangle.Height - 1);

                var drawStringRectangle = new Rectangle(
                    e.ClipRectangle.X + 5,
                    e.ClipRectangle.Y + 3,
                    e.ClipRectangle.Width,
                    e.ClipRectangle.Height);

                e.Graphics.Clear(this.BackColor);

                var font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                e.Graphics.FillRectangle(SystemBrushes.GradientInactiveCaption, fillRectangle);
                e.Graphics.DrawString("Ir", font, Brushes.Black, drawStringRectangle);

                using (Pen p = new Pen(SystemBrushes.Highlight)) {
                    e.Graphics.DrawRectangle(p, drawRectangle);
                }

            }
        }

        protected override void OnMouseEnter(EventArgs e) {
            base.OnMouseEnter(e);
            IsMouseOver = Hittest(this.PointToClient(MousePosition));
        }

        protected override void OnMouseLeave(EventArgs e) {
            base.OnMouseLeave(e);
            IsMouseOver = Hittest(this.PointToClient(MousePosition));
        }

        protected override void OnMouseHover(EventArgs e) {
            base.OnMouseHover(e);
            IsMouseOver = Hittest(this.PointToClient(MousePosition));
        }

        #endregion

        #region Properties

        private bool _isMouseOver = false;
        private bool IsMouseOver {
            get {
                return _isMouseOver;
            }
            set {
                if (_isMouseOver != value) {
                    _isMouseOver = value;
                    InvalidateEx();
                }
            }
        }

        #endregion

        #region Methods

        protected void InvalidateEx() {
            if (Parent == null)
                return;

            Rectangle rc = new Rectangle(this.Location, this.Size);
            Parent.Invalidate(rc, true);
        }

        private bool Hittest(Point point) {
            bool hit = false;

            if (point.X >= 0 && point.X < this.Width &&
                point.Y >= 0 && point.Y < this.Height) {
                hit = true;
            }

            return hit;
        }

        #endregion
    }
}
